package com.jvm.方法区内存溢出;

import jdk.internal.org.objectweb.asm.ClassWriter;
import jdk.internal.org.objectweb.asm.Opcodes;

//-XX:MaxMetaspaceSize=16M
//Metaspace元空间内存溢出
public class MyClassLoader extends ClassLoader {
    public static void main(String[] args) {
        int j = 0;
        try {
            //创建类加载器
            MyClassLoader myClassLoader = new MyClassLoader();
            //cglib动态底层字节码技术
            for(int i =0;i<100000;i++,j++){
                //字节码动态生成器对象
                ClassWriter classWriter = new ClassWriter(0);
                /*
                    版本号
                    类的访问修饰符
                    类名
                    类的包名
                    类的父类
                    类的接口
                 */
                classWriter.visit(Opcodes.V1_8,Opcodes.ACC_PUBLIC,"Proxy"+i,null,"java/lang/Object",null);
                //生成类的二进制字节码
                byte[] code = classWriter.toByteArray();
                //加载类
                myClassLoader.defineClass("Proxy"+i,code,0,code.length);
            }
        } finally {
            System.out.println(j);
        }


    }
}
